package com.neusoft.data.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.data.mapper.SerKeyWordMapper;
import com.neusoft.data.domain.SerKeyWord;
import com.neusoft.data.service.ISerKeyWordService;

/**
 * 关键词Service业务层处理
 * 
 * @author slh
 * @date 2023-12-29
 */
@Service
public class SerKeyWordServiceImpl implements ISerKeyWordService 
{
    @Autowired
    private SerKeyWordMapper serKeyWordMapper;

    /**
     * 查询关键词
     * 
     * @param keyWordId 关键词主键
     * @return 关键词
     */
    @Override
    public SerKeyWord selectSerKeyWordByKeyWordId(Long keyWordId)
    {
        return serKeyWordMapper.selectSerKeyWordByKeyWordId(keyWordId);
    }

    /**
     * 查询关键词列表
     * 
     * @param serKeyWord 关键词
     * @return 关键词
     */
    @Override
    public List<SerKeyWord> selectSerKeyWordList(SerKeyWord serKeyWord)
    {
        return serKeyWordMapper.selectSerKeyWordList(serKeyWord);
    }

    /**
     * 新增关键词
     * 
     * @param serKeyWord 关键词
     * @return 结果
     */
    @Override
    public int insertSerKeyWord(SerKeyWord serKeyWord)
    {
        serKeyWord.setCreateTime(DateUtils.getNowDate());
        return serKeyWordMapper.insertSerKeyWord(serKeyWord);
    }

    /**
     * 修改关键词
     * 
     * @param serKeyWord 关键词
     * @return 结果
     */
    @Override
    public int updateSerKeyWord(SerKeyWord serKeyWord)
    {
        serKeyWord.setUpdateTime(DateUtils.getNowDate());
        return serKeyWordMapper.updateSerKeyWord(serKeyWord);
    }

    /**
     * 批量删除关键词
     * 
     * @param keyWordIds 需要删除的关键词主键
     * @return 结果
     */
    @Override
    public int deleteSerKeyWordByKeyWordIds(Long[] keyWordIds)
    {
        return serKeyWordMapper.deleteSerKeyWordByKeyWordIds(keyWordIds);
    }

    /**
     * 删除关键词信息
     * 
     * @param keyWordId 关键词主键
     * @return 结果
     */
    @Override
    public int deleteSerKeyWordByKeyWordId(Long keyWordId)
    {
        return serKeyWordMapper.deleteSerKeyWordByKeyWordId(keyWordId);
    }

    @Override
    public String importUser(List<SerKeyWord> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户不存在");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (SerKeyWord serKeyWord : list) {
            try {
                SerKeyWord k = serKeyWordMapper.selectSerKeyWordByKeyWordId(serKeyWord.getKeyWordId());

                if (StringUtils.isNull(k)) {
                    serKeyWordMapper.insertSerKeyWord(serKeyWord);
                    successNum ++;
                    successMsg.append("<br/>" + successNum + "、会话 " + serKeyWord.getKeyWordName() + " 导入成功：");
                } else if (updateSupport) {
                    serKeyWordMapper.updateSerKeyWord(serKeyWord);
                    successMsg.append("<br/>" + successNum + "、会话 " + serKeyWord.getKeyWordName() + " 更新成功：");
                } else {
                    failureNum ++;
                    failureMsg.append("<br/>" + failureNum + "、会话 " + serKeyWord.getKeyWordName() + " 已存在：");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、关键词 " + serKeyWord.getKeyWordName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下");
            throw  new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功共 " + successNum + " 条数据");
        }

        return successMsg.toString();
    }
}
